/**
 * Copyright (C), 2015, 上海赛可电子商务有限公司
 * Author:   张禀奇
 * Date:     2015-11-23
 * lever:    page
 * Description: 首页js
 */

;(function($,window) {

	homePage = {
		hthis:this,
		brandAjaxUrl:"getVelBrandId.htm",
		scopeAjaxUrl:"getVelFQ.htm",
		init:function(){

			$(document).on("click",function(){
				$(".select-items").css("display","none");
			});
			this.budgetSwitch();
			this.customSelectBox();
			this.addSelectBox(this.brandAjaxUrl);
			this.verifySelectFunc();

			//咨询反馈
			this.comp();
		},
		budgetSwitch:function(){
			var _self = this;
			$(".ofc-tab-btn").on("click",function(){
				$("#item-brand").val("");
				$("#item-vehicle").val("");
//				$("#item-scope").val("");
				$(".selected-item[selected-type='brand']").html("请选择品牌");
				$(".selected-item[selected-type='vehicle']").html("请选择车系");
//				$(".selected-item[selected-type='scope']").html("请选择范围");
				var thisTypeName = $(this).attr("btn-type");
				$(".ofc-tab-btn").removeClass("ofc-tab-cur");
				$(this).addClass("ofc-tab-cur");
				$(".ofc-search-items").children("div").css("display","none");
				$(".ofc-search-items").children(".ofc-tab-item-"+thisTypeName).css("display","block");
				if(thisTypeName == "budget"){
					if($(".select-items[item-type='scope']").length > 0){
						return false;
					}else{
						_self.addSelectBox(_self.scopeAjaxUrl);
					}
				}
			});
		},
		addSelectBox:function(type,agr){
			var _self = this;
			if(type=="getVelBrandId.htm"){
				$.ajax({
					type: "GET",
					url: type,
					dataType: "json",
					success: function(data){
						$("#item-brand").parent().append("<ul class='select-items' item-type='brand' style='display:none'><li class='item'>请选择品牌</li></ul>");
						$(data).each(function(i,item){
							$(".select-items[item-type='brand']").append("<li class='item' item-id="+item.id+">"+item.name+"</li>");
						});
						$(".select-items[item-type='brand'] .item").bind("click",function(){
							_self.selectItem(this,"brand","getVelseries.htm");
						});
					}
	         			});
		         	}else if(type=="getVelseries.htm"){
		         		var _url = type+"?velBrandId="+agr;
		         		$.ajax({
							type: "GET",
							url: _url,
							dataType: "json",
							success: function(data){
								if($(".select-items[item-type='vehicle']").length>0){
									$(".select-items[item-type='vehicle']").remove();
								}
								$("#item-vehicle").parent().append("<ul class='select-items' item-type='vehicle' style='display:none'><li class='item'>请选择车系</li></ul>");
								$(data).each(function(i,item){
									$(".select-items[item-type='vehicle']").append("<li class='item' item-id="+item.id+">"+item.name+"</li>");
								});
								$(".select-items[item-type='vehicle'] .item").bind("click",function(){
									_self.selectItem(this,"vehicle");
								});
							}
			         	});
		         	}else if(type=="getVelFQ.htm"){
		         		$.ajax({
							type: "GET",
							url: type,
							dataType: "json",
							success: function(data){
								var firstTiny =" ";
								var secondTiny = " ";
								//价格区间的html内容数组
								var priceRangeHtml = new Array();
								var oneItem = "";
								$("#item-scope").parent().append("<ul class='select-items' item-type='scope' style='display:none'><li class='item'>请选择范围</li></ul>");
								$(data).each(function(i,item){
									//应需求方要求，下拉框不显示
		//							$(".select-items[item-type='scope']").append("<li class='item' item-id="+item.id+">"+item.name+"</li>");
									oneItem = "<span class='car-detail'><a href="+MALL.base+"/list/fq-0-0-0-" + item.parentId + "_"+item.id+"-0-0-0-0-0-0-0-"+$("#cityId").val()+"-0-1-1.html>" + item.name + "</a></span>";
									if(oneItem==="undefined"){
										oneItem = " ";
									}else{
										oneItem += "&nbsp;&nbsp;&nbsp;";
										priceRangeHtml.push(oneItem);
									}
								});
								//显示前两条 价格
		//						$(".ofc-show-tiny").html(firstTiny+"&nbsp;&nbsp;&nbsp;"+ secondTiny);
								//显示所有的价格区间
								$(".ofc-show-tiny").html(priceRangeHtml.toString().replace(/\,/g,""));
								$(".select-items[item-type='scope'] .item").bind("click",function(){
									_self.selectItem(this,"scope");
								});
							}
			         	});
		         	}
		},
		customSelectBox:function(type){
			$(".selected-item").on("click",function(e){
				e.preventDefault();
				e.stopPropagation();
				$(".select-error").html("");
				$(".select-items").css("display","none");
				var elmType = $(this).attr("selected-type");
				if(elmType == "vehicle"){
					if($(".select-items[item-type='vehicle']").length == 0){
						$(".select-error").html("请先选择品牌");
					}
				}
				$(".select-items[item-type="+elmType+"]").toggle();
			});
		},
		selectItem:function(elm,type,url){
			$(".selected-item[selected-type='vehicle']").html("请选择车系");
			$(".ofc-search-btn[search-type='vehicle']").attr("href","javascript:;");
			if(type=="brand"){
				if($(elm).index() == 0){
					$(elm).parent().prev().html($(elm).html());
					$("#item-"+type).val('');
					$("#item-vehicle").val('');
					$(".selected-item[selected-type='vehicle']").html("请选择车系");
					$(".select-items[item-type='vehicle']").remove();
				}
			}
			if($(elm).parent().prev().html() == $(elm).html()){
				return false;
			}else{
				$(elm).parent().prev().html($(elm).html());
				$("#item-"+type).val($(elm).attr("item-id"));
				this.addSelectBox(url,$(elm).attr("item-id"));
			}
			if(type=="vehicle"){
				$(".ofc-search-btn[search-type='vehicle']").attr("href",MALL.base+"/list/fq-"+$("#item-brand").val()+"-"+$("#item-vehicle").val()+"-0-0-0-0-0-0-0-0-0-"+$("#cityId").val()+"-0-1-1.html");
			}
			if(type=="scope"){
				//修第一次选中，再置空后，点搜索 跳走
				if($("#item-scope").val() == ""){
					$(".ofc-search-btn[search-type='budget']").attr("href","javascript:;");
				}else{
					$(".ofc-search-btn[search-type='budget']").attr("href",MALL.base+"/list/fq-0-0-0-2_"+$("#item-scope").val()+"-0-0-0-0-0-0-0-"+$("#cityId").val()+"-0-1-1.html");
				}
			}
 		},
		verifySelectFunc:function(){
			$(".ofc-search-btn").click(function(){

				if($(this).attr("search-type") == "vehicle"){
					if($("#item-brand").val() == ""){
						$(".ofc-search-btn[search-type='vehicle']").attr("href",MALL.base+"/list/fq-0-0-0-0-0-0-0-0-0-0-0-"+$("#cityId").val()+"-0-1-1.html");
					}else if($("#item-vehicle").val() == ""){
						$(".ofc-search-btn[search-type='vehicle']").attr("href",MALL.base+"/list/fq-"+$("#item-brand").val()+"-0-0-0-0-0-0-0-0-0-0-"+$("#cityId").val()+"-0-1-1.html");
					}else{
						$(".ofc-search-btn[search-type='vehicle']").attr("href",MALL.base+"/list/fq-"+$("#item-brand").val()+"-"+$("#item-vehicle").val()+"-0-0-0-0-0-0-0-0-0-"+$("#cityId").val()+"-0-1-1.html");
					}
				}else if($(this).attr("search-type") == "budget"){
					if($("#item-scope").val() == ""){
						$(".ofc-search-btn[search-type='budget']").attr("href",MALL.base+"/list/fq-0-0-0-0-0-0-0-0-0-0-0-"+$("#cityId").val()+"-0-1-1.html");
					}
				}
			});

		},
		comp:function(){

		            if($('#search-comp').length <= 0){
		                return false;
		            };

		            $('#search-comp').on('click',function(){
		                sidebar.openFeedbook();
		            });

		}
	};
	homePage.init();
	ECar.fullSlide({'ele':'banner-wrap','showmarkers':true,'showcontrols':false,'autoplay':false,'animspeed':'7000'});
	if($('.page-num span').length<=1){
		$('.page-num').hide();
	}
})(jQuery,window);

